package com.iwhalecloud.bss.kite.cucc.web.controller.dcpublic;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author linjj
 * version 1.0 2021/12/27
 */

@RestController
@RequestMapping({"/localDcPublic/data"})
@Api(
    tags = {"/localDcPublic/data - dcPublic数据"}
)
public class LocalDcPublicController {

    @ApiOperation(
        value = "根据stype和pkey查询dc_public",
        notes = "根据stype和pkey查询dc_public"
    )
    @ApiImplicitParams({@ApiImplicitParam(
        name = "stype",
        value = "stype",
        required = true
    ), @ApiImplicitParam(
        name = "pkey",
        value = "pkey",
        required = true
    )})
    @GetMapping
    public DataResult<DcPublic> getDcPublic(@RequestParam String stype, @RequestParam String pkey) {
        List<DcPublic> list = DcPublicCache.getInst().defaultGetObject(stype);
        if (KiteListUtils.isNotEmpty(list)) {
            List<String> goodsIds = list.stream()
                .filter((d) -> Objects.equals(pkey, d.getPkey()))
                .map(DcPublic::getCodea)
                .filter(s -> KiteStringUtils.isNotEmpty(s))
                .collect(Collectors.toList());
            String join = KiteStringUtils.join(goodsIds, ",");
            list.get(0).setCodea(join);
            return ErrorConsts.SUCCESS.getResult(list.get(0));
    }
        return ErrorConsts.SUCCESS.getResult(null);
    }
}
